import operator


class ExprMixin(object):
    __slots__ = []
    def __add__(self, other):
        return BinExpr(operator.add, self, other, "+")
    def __sub__(self, other):
        return BinExpr(operator.sub, self, other, "-")
    def __mul__(self, other):
        return BinExpr(operator.mul, self, other, "*")
    def __div__(self, other):
        return BinExpr(operator.div, self, other, "/")
    def __mod__(self, other):
        return BinExpr(operator.mod, self, other, "%")
    def __pow__(self, other):
        return BinExpr(operator.pow, self, other, "**")
    def __radd__(self, other):
        return BinExpr(operator.add, other, self, "+")
    def __rsub__(self, other):
        return BinExpr(operator.sub, other, self, "-")
    def __rmul__(self, other):
        return BinExpr(operator.mul, other, self, "*")
    def __rdiv__(self, other):
        return BinExpr(operator.div, other, self, "/")
    def __rmod__(self, other):
        return BinExpr(operator.mod, other, self, "%")
    def __pow__(self, other):
        return BinExpr(operator.pow, other, self, "**")

class BinExpr(ExprMixin):
    __slots__ = ["op", "lhs", "rhs", "opname"]
    def __init__(self, op, lhs, rhs, opname):
        self.op = op
        self.opname = opname
        self.lhs = lhs
        self.rhs = rhs
    def __repr__(self):
        return "%r %s %r" % (self.lhs, self.opname, self.rhs)
    def __call__(self, context):
        lhs = self.lhs(context) if callable(self.lhs) else self.lhs
        rhs = self.rhs(context) if callable(self.rhs) else self.rhs
        return self.op(lhs, rhs)

class Path(ExprMixin):
    __slots__ = ["__name", "__parent"]
    def __init__(self, name, parent = None):
        self.__name = name
        self.__parent = parent
    def __repr__(self):
        if self.__parent is None:
            return self.__name
        return "%r.%s" % (self.__parent, self.__name)
    def __call__(self, context):
        if self.__parent is None:
            return context
        context2 = self.__parent(context)
        return context2[self.__name]
    def __getattr__(self, name):
        return Path(name, self)


this = Path("ctx")


if __name__ == "__main__":
    x = (this.foo * 2 + 3) % 11
    print x
    print x({"_" : {"foo" : 7}})














